﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebUtils;
using TTQT;
using TTQT.TTQT_Code;
public partial class DMKH : MasterControl//System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid();
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid()
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select id, case when gioitinh=0 then N'Nữ' else 'Nam' end as GioiTinh,Ten,DiaChi,Tel,fax,Email from KHNCC where type=0 and MaCN='" + UserInfo.Brn_Code + "'";
            datagrid.DataSourceID = "ds";
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        this.txtId.Text = string.Empty;
        this.txtTen.Text = string.Empty;
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng

            if (txtId.Text.Length > 0)
            {
                ds.UpdateCommand = "update KHNCC set Ten=N'" + txtTen.Text + "',GioiTinh=" + cboGioiTinh.SelectedValue + ",DiaChi=N'" + txtDiaChi.Text + "',tel='" + txtTel.Text + "',email='" + txtEmail.Text + "',ghiChu=N'" + txtGhiChu.Text + "' where Id=" + txtId.Text + "";
                if (ds.Update() > 0)
                {
                    bindGrid();
                    lblUpdateStatus.Text = "";
                }
                visiblePanels(true, false);
                lblError.Text = "";
            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else
            {
                ds.InsertCommand = "insert into KHNCC(Ten,GioiTinh,DiaChi,Tel,email,ghiChu,MaCN) values(N'" + txtTen.Text + "'," + cboGioiTinh.SelectedValue + ",N'" + txtDiaChi.Text + "','" + txtTel.Text + "','" + txtEmail.Text + "',N'" + txtGhiChu.Text + "','" + UserInfo.Brn_Code + "')";
                if (ds.Insert() > 0)
                {
                    bindGrid();
                    lblUpdateStatus.Text = "";
                }
                else
                    lblUpdateStatus.Text = "Có lỗi khi thêm mới KH";
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Trung ma. Nhap lai! <br />" + ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from KHNCC where id=" + e.Item.Cells[0].Text;
            DataTable hehe = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();

            hehe.Rows[0][1].ToString();
            txtId.Text = hehe.Rows[0][0].ToString();
            try
            {
                cboGioiTinh.SelectedValue = hehe.Rows[0][1].ToString();
            }
            catch (Exception)
            {
                
            }
            txtTen.Text = hehe.Rows[0][2].ToString();
            txtDiaChi.Text = hehe.Rows[0][3].ToString();
            txtTel.Text = hehe.Rows[0][4].ToString();
            txtEmail.Text = hehe.Rows[0]["Email"].ToString();
            txtGhiChu.Text = hehe.Rows[0]["GhiChu"].ToString();
            visiblePanels(false, true);
        }
        else if (e.CommandName == "cmdDelete")
        {
            DataView kq;
            ds.SelectCommand = "select count(*) from hoadon_Master where KHNCCID='" + e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);

            if (!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page, "Đã có giao dịch xuất (nhập) liên quan nên không xóa được.");
                bindGrid();
                return;
            }
            ds.DeleteCommand = "delete KHNCC where ID='" + e.Item.Cells[0].Text + "'";
            ds.Delete();
            bindGrid();

        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", "delete_Confirm");
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
